package com.wfuhui.modules.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wfuhui.common.utils.R;
import com.wfuhui.modules.api.annotation.LoginUser;
import com.wfuhui.modules.oms.entity.UserEntity;
import com.wfuhui.modules.oms.entity.OrderEntity;
import com.wfuhui.modules.oms.service.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/api/order")
@Api("订单接口")
public class ApiOrderController {
	
	@Autowired
	private OrderService orderService;
	
	
    @GetMapping("getOrderList")
    @ApiOperation(value = "获取订单信息")
    public R getOrderList(@LoginUser UserEntity user, Integer status){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("orderStatus", status);
    	params.put("userId", user.getId());
    	List<OrderEntity> orderList = orderService.queryList(params);
        return R.ok().put("orderList", orderList);
    }
    
    @PostMapping("createOrder")
    @ApiOperation(value = "创建订单")
    public R createOrder(@LoginUser UserEntity user, @RequestBody OrderEntity orderEntity){
    	orderEntity.setUserId(user.getId());
    	orderEntity.setCreateTime(new Date());
    	orderEntity.setOrderStatus(1);
    	orderService.createOrder(orderEntity);
    	return R.ok();
    }
    
    @GetMapping("orderDetail")
    @ApiOperation(value = "订单详情")
    public R getOrderDetail(Integer id) {
    	OrderEntity order = orderService.queryObject(id);
    	return R.ok().put("order", order);
    }
    
    @GetMapping("cancelOrder")
    @ApiOperation(value = "取消订单")
    public R cancelOrder(Integer id) {
    	OrderEntity order = new OrderEntity();
    	order.setOrderStatus(0);
    	order.setOrderId(id);
    	orderService.update(order);
    	return R.ok();
    }
    
    @GetMapping("confirmOrder")
    @ApiOperation(value = "完成订单")
    public R confirmOrder(Integer id) {
    	OrderEntity order = new OrderEntity();
    	order.setOrderStatus(4);
    	order.setOrderId(id);
    	orderService.update(order);
    	return R.ok();
    }
    
    @GetMapping("recharge")
    public R recharge(@LoginUser UserEntity user, Integer amount) {
    	orderService.recharge(user.getId(), amount);
    	return R.ok();
    }
    
    @GetMapping("statistics")
    @ApiOperation(value = "统计")
    public R statistics() {
    	
    	return R.ok();
    }
    
}
